﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using MaxLeaf.Platform.DALImplementation;
using MaxLeaf.Platform.Functions;
using CommonsLib.Utility;
using MaxLeaf.Platform.Commons;
using MaxLeaf.Platform.Contract;
using System.Transactions;
using CommonsLib.Utility;
using CommonsLib.Settings;

namespace MaxLeaf.Platform.BLLImplementation
{
    public class poOrderRecvProcessor :   IPoOrderRecvProcess
    {
         /// <summary>
         /// 提交入库
         /// </summary>
         /// <param name="intEnterUserId"></param>
         /// <param name="intRecvHdrID"></param>
         /// <param name="lstRecvDtlKeys"></param>
         /// <returns></returns>
        public imOthersInputHdrDataModel Post(int intEnterUserId, int intRecvHdrID, List<int> lstRecvDtlKeys)
        {
 
            poOrderRecvHdrBLL bllRecvHdr = new poOrderRecvHdrBLL();
            poOrderRecvDtlBLL bllRecvDtl = new poOrderRecvDtlBLL(); ;
            if (lstRecvDtlKeys == null || lstRecvDtlKeys.Count == 0)
            {
                //得到unposted dtl
                lstRecvDtlKeys = bllRecvDtl.GetUnpostKeys(intRecvHdrID);
                if (lstRecvDtlKeys == null || lstRecvDtlKeys.Count == 0)
                {
                    throw new MaxLeafException(string.Format(Const.MSG_NoDataToPost, intRecvHdrID));
                }
            }

            poOrderRecvHdrDataModel modelRecvHdr = new poOrderRecvHdrDataModel();
            modelRecvHdr = (poOrderRecvHdrDataModel)bllRecvHdr.Select(modelRecvHdr, intRecvHdrID);
            if (modelRecvHdr.Posted)
            {
                throw new MaxLeafException(string.Format(Const.MSG_NoDataToPost, intRecvHdrID));
            }

            BaseBLL bllOthersInputHdr = BaseBLL.GetDataModelBLL("imOthersInputHdrDataModel");
            imOthersInputDtlBLL bllOthersInputDtl = new imOthersInputDtlBLL();

            //插入imOthersInputHdr.Finterid
            imOthersInputHdrDataModel modelOthersInputHdr = Post_InitHdr(bllOthersInputHdr, modelRecvHdr, intEnterUserId);

            List<imOthersInputDtlDataModel> lstOrderDtl = new List<imOthersInputDtlDataModel>();
            for (int i = 0; i < lstRecvDtlKeys.Count; i++)
            {

                poOrderRecvDtlDataModel modelRecvDtl = new poOrderRecvDtlDataModel();
                modelRecvDtl = (poOrderRecvDtlDataModel)bllRecvDtl.Select(modelRecvDtl, lstRecvDtlKeys[i]);
                //插入imOthersInputHdr.Finterid
                lstOrderDtl.Add(Post_InitDtl(bllOthersInputDtl, modelOthersInputHdr.Key, modelRecvDtl));

            }

            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
                using (DBAccess dbConOrder = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPSEOrder)))
                {
                    try
                    {
                        //dbConOrder.BeginTrans();

                        if (!bllRecvHdr.CurrentBRL.CanPost(dbConOrder, bllRecvHdr.CurrentDAL, "poOrderRecvHdr", "FinterId", intRecvHdrID))
                        {
                            throw new MaxLeafException(string.Format(Const.MSG_NoDataToPost, intRecvHdrID));
                        }
                        //保存 imOthersInputHdr 其他数据
                        modelOthersInputHdr = (imOthersInputHdrDataModel)bllOthersInputHdr.Update(dbConOrder, modelOthersInputHdr);



                        for (int i = 0; i < lstRecvDtlKeys.Count; i++)
                        {
                            if (bllRecvDtl.CurrentBRL.CanPost(dbConOrder, bllRecvDtl.CurrentDAL, "poOrderRecvDtl", "DtlId", lstRecvDtlKeys[i]))
                            {
                                //保存 imOthersInputDtl 其他数据
                                lstOrderDtl[i] = (imOthersInputDtlDataModel)bllOthersInputDtl.Update(dbConOrder, lstOrderDtl[i]);
                                //写入来源 RecvDtl key
                                bllOthersInputDtl.SetRecvDtlId(dbConOrder, lstOrderDtl[i].Key, intRecvHdrID, lstRecvDtlKeys[i]);

                                //设置RecvDtl Post标志
                                bllRecvDtl.SetPosted(dbConOrder, lstRecvDtlKeys[i], lstOrderDtl[i].ForeignKey, lstOrderDtl[i].Key);
                            }

                        }
                        //设置RecvHdr Post标志
                        bllRecvHdr.SetPosted(dbConOrder, intRecvHdrID);

                        //dbConOrder.CommitTrans();
                    }
                    catch(Exception ex)
                    {
                        //dbConOrder.RollbackTrans();
                        LogManager.Debug(ex.Message);
                        throw;
                    }
                }
                new InputProcessor().AutoLocAppro(modelOthersInputHdr);

                ts.Complete();
            }
            

            
            return modelOthersInputHdr;

            
        }





        private imOthersInputDtlDataModel Post_InitDtl(imOthersInputDtlBLL bllRecvDtl, int intForeignKey, poOrderRecvDtlDataModel modelRecvDtl)
        {
            if (modelRecvDtl.Qty.CompareTo(0d)<=0)
            {
                throw new MaxLeafException("数量不能为0");
            }

            imOthersInputDtlDataModel modelDtl = new imOthersInputDtlDataModel(
                intForeignKey
                , modelRecvDtl.ItemId
                , modelRecvDtl.UnitId
                , modelRecvDtl.Qty
                , modelRecvDtl.WebPrice
                , modelRecvDtl.SubTotal * modelRecvDtl.Qty / modelRecvDtl.ReqQty
                 , modelRecvDtl.Cost
                );

            return (imOthersInputDtlDataModel)bllRecvDtl.InsertFinterID(modelDtl);
        }

        private imOthersInputHdrDataModel Post_InitHdr(BaseBLL bllOthersInputHdr, poOrderRecvHdrDataModel modelRecvHdr, int EnterUserID)
        {


            int DocType = new mInputTypeBLL().GetRecvTypeId(modelRecvHdr.CompanyId);
            imOthersInputHdrDataModel modelHdr = new imOthersInputHdrDataModel(
                modelRecvHdr.CompanyId
                , CvtFunc.ToInt( BaseBLL.GetDataModelBLL("mUser_InfoDataModel").GetFieldValue(new mUser_InfoDataModel(),EnterUserID, "DepId"))
                , DateTime.Now
                , DocType
                , modelRecvHdr.RecvUserId
                , EnterUserID
               );
            return (imOthersInputHdrDataModel)bllOthersInputHdr.InsertFinterID(modelHdr);
        }




        public bool UnPost(int intOthersInputHdrID)
        {


            imOthersInputHdrBLL bllOthersInputHdr = new imOthersInputHdrBLL();
            imOthersInputDtlBLL bllOthersInputDtl = new imOthersInputDtlBLL();

            imOthersInputHdrDataModel modelOthersInputHdr = (imOthersInputHdrDataModel)bllOthersInputHdr.Select(new imOthersInputHdrDataModel(), intOthersInputHdrID);
            if (modelOthersInputHdr == null || modelOthersInputHdr.Is_Delete || modelOthersInputHdr.Posted)
            {
                throw new MaxLeafException(string.Format(Const.MSG_NoDataToUnPost, intOthersInputHdrID));
            }

            List<int> lstOthersInputDtlKeys = bllOthersInputDtl.GetUnpostKeys(intOthersInputHdrID);
            if (lstOthersInputDtlKeys == null || lstOthersInputDtlKeys.Count == 0)
            {
                throw new MaxLeafException(string.Format(Const.MSG_NoDataToUnPost, intOthersInputHdrID));
            }

            poOrderRecvHdrBLL bllRecvHdr = new poOrderRecvHdrBLL();
            poOrderRecvDtlBLL bllRecvDtl = new poOrderRecvDtlBLL(); ;
             
             

            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
                using (DBAccess dbConOrder = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPSEOrder)))
                {
                    try
                    {
                        List<int> lstRecvHdrKeys = new List<int>();
                        imOthersInputDtlDataModel modelOthersInputDtl_tmp=new imOthersInputDtlDataModel();
                        for (int i = 0; i < lstOthersInputDtlKeys.Count; i++)
                        {
                            int intRecvHdrId, intRecvDtlId;
                            double dblRecvQty;
                            bllOthersInputDtl.GetRecvData(dbConOrder, lstOthersInputDtlKeys[i],out intRecvHdrId,out intRecvDtlId,out dblRecvQty);

                            bllRecvDtl.SetUnPosted(dbConOrder, intRecvHdrId, intRecvDtlId, dblRecvQty);
                            bllOthersInputDtl.SetUnRecv(dbConOrder, lstOthersInputDtlKeys[i]);

                            if (!lstRecvHdrKeys.Contains(intRecvHdrId))
                            {
                                lstRecvHdrKeys.Add(intRecvHdrId);
                            }
                        }
                        for (int i = 0; i < lstRecvHdrKeys.Count; i++)
                        {
                            bllRecvHdr.SetUnPosted(dbConOrder, lstRecvHdrKeys[i]);
                        }
                        bllOthersInputHdr.SetUnRecv(dbConOrder, intOthersInputHdrID);

                    
                    }
                    catch
                    {
 
                        throw;
                    }
                }
                

                ts.Complete();
            }



            return true;
        }
    }
}
